from re import T
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from location import location_A
from basepage import BasePage
import time,random
from ini import ini
from dd import dd
import _thread
# import threading


class login_C:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get(url='https://xhwtpc.cmea.org.cn')
        # self.driver.get(url='https://xhwtpc.cmea.org.cn/#/')
        self.driver.maximize_window()
        self.loc = location_A()
        self.base = BasePage(self.driver)
    def login_A(self, name='', passs=''):
        self.base.click_element(loc=self.loc.login)
        # self.base.action_click(loc=self.loc.login)
        self.base.click_element(loc=self.loc.password_login)
        self.base.input_text(text=name, loc=self.loc.name)
        self.base.input_text(text=passs, loc=self.loc.password)
        self.base.click_element(loc=self.loc.select_table)
        self.base.click_element(loc=self.loc.login_button)
        self.driver.get(url='https://xhwtpc.cmea.org.cn/#/detail?Id=5579&ProductType=1005')
        time.sleep(2)
        self.base.execute_js(dis=500)


class look:
    def __init__(self, name='', passs=''):
        driver = login_C()
        driver.login_A(name, passs)
        self.loc = location_A()
        self.base = BasePage(driver.driver)
    def look(self):
        for i in range(8,100):
            # 第七章
            if i<=8:
                chapter = '//*[@id="root"]/div/div/div[2]/div/div[3]/div[1]/div/div/div[3]/div[2]/div[2]/div/div/div[{}]'.format(i)
            else:
                chapter = '//*[@id="root"]/div/div/div[2]/div/div[2]/div[1]/div/div/div[3]/div[2]/div[2]/div/div/div[{}]'.format(i)

            self.base.click_element(loc=(By.XPATH, '{}'.format(chapter)))
            time.sleep(1)
            try:
                for j in range(1,5):
                    if i <=8:
                        if j<=1:
                            # 第一节
                            bar = '//*[@id="root"]/div/div/div[2]/div/div[3]/div[1]/div/div/div[3]/div[2]/div[2]/div/div/div[{}]/div[2]/div/div/div[{}]/div'.format(i,j)
                        else:
                            bar = '//*[@id="root"]/div/div/div[2]/div/div[2]/div[1]/div/div/div[3]/div[2]/div[2]/div/div/div[{}]/div[2]/div/div/div[{}]/div'.format(i,j)
                    else:
                        bar = '//*[@id="root"]/div/div/div[2]/div/div[2]/div[1]/div/div/div[3]/div[2]/div[2]/div/div/div[{}]/div[2]/div/div/div[{}]/div'.format(
                            i, j)

                    self.base.click_element(loc=(By.XPATH,'{}'.format(bar)))
                    time.sleep(3)
                    self.base.execute_js_return("return document.getElementsByTagName('video')[0].duration;")
                    time.sleep(40*60)
                    # time.sleep(10)
                    print('第{}章，第{}节'.format({i-1},{j}))
            except:
                break
            self.base.execute_js(dis=200)


def test_r(name, passs, t):
    print(name,  '   '+passs)
    time.sleep(4)
    seeOk = ini.setSeeOk(name)
    t.release()  # 释放锁





if __name__ == '__main__':

    ini = ini();
    configs = ini.getNamePass()
    thread_list = []
    locks = []

    for config in configs:
        par = {};
        has_name = ini.hasSeeOk(config['name'])
        if has_name:
            print('name is exist, skip')
            continue
        lock = _thread.allocate_lock()  # 分配锁对象
        lock.acquire()  # 获取锁对象
        par['name'] = config
        par['lock'] = lock
        locks.append(par)

    for i in locks:
        _thread.start_new_thread(test_r, (i['name']['name'], i['name']['pass'], i['lock']))

    for i in locks:
        while(i['lock'].locked()):
            pass

    print(configs)






# if __name__ == '__main__':
#     ini = ini();
#     configs = ini.getNamePass()
#     thread_list = []
#     for config in configs:
#         has_name = ini.hasSeeOk(config['name'])
#         if has_name:
#             print('name is exist, skip')
#             continue
#         # thread1 = threading.Thread(target=test_r(config['name'], config['pass']))
#         # thread_list.append(thread1)
#         # # thread1.setDaemon(True)
#         # thread1.start()
#         # # thread1.join()
#         _thread.start_new_thread(test_r, (config['name'], config['pass']))
#         #l = look()
#         #l.look()

#     time.sleep(1)
#     print(configs)





